home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group99a.txt
/
000219_icon-group-sender _Thu Oct 21 15:43:50 1999.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
3KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id PAA05509
for icon-group-addresses; Thu, 21 Oct 1999 15:42:36 -0700 (MST)
Message-Id: <199910212242.PAA05509@baskerville.CS.Arizona.EDU>
Date: Thu, 21 Oct 1999 09:01:50 -0700
From: Steve Wampler <swampler@noao.edu>
X-Accept-Language: en
CC: icon-group@optima.CS.Arizona.EDU
Subject: Re: List subscription insert/removal
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
Blake Chapman wrote:
>
> Icon Project,
>
> I am trying to figure out how to insert a new element into a list,
> and remove an element from the middle of a list (not the end).
> In answer to this question previously Gordon Peterson sent
> a reply. I tried what he said and it doesn't work.
>
> My original question, that he answered, was
> "I find no routines to pluck one element out of the middle of a list, or insert
> one in. Are there such routines?".
...
> procedure main()
> local s # it will be a list
>
> s:= ["a", "b", "c", "d"]
> WriteL(s) # this writes "abcd" as it should
> # Next, I want to insert "x" as a list element between "b" and "c" ....
> # didn't work: s[3:3]:="x"
> # didn't work: s[3:3] := ["x"]
> # didn't work: s[3+:0] := ["x"]
> s[3+:0] := "x" # didn't work; I get a run-time error and program abort
> WriteL(s)
> end
Unfortunately, sublists are not variables, as you've seen with your test
cases. So trying to assign to a sublist isn't going to work, no matter
how you try and construct it. I'm not sure why sublists are implemented
this way, but can imagine all sorts of difficulties that might arise from
implementing it otherwise. (I've often thought that it would be nice
to implement lists so they are more consistent with strings, but the result,
while perhaps quite nice, would no longer be Icon... Perhaps someone with
more time on their hands would like to think about this more? (List scanning
would become easier to implement, among other things.)
Anyway, you have to implement the operations you want by constructing
new lists:
procedure replace(a, start, stop, value)
return a[1:start] ||| value ||| a[stop:0]
end
where the call would be:
s := replace(s, 3, 3, ["x"])
I agree that this is not as clean as one might hope for, but I don't
think there's any way around this.
Because of the call-by-value semantics of Icon, you have
to do the "pluck" (where I assume you mean to remove the elements
from the list when you pluck them) as a two step process:
a := s[3:5]
s := replace(s, 3, 5, [])
--
Steve Wampler- SOLIS Project, National Solar Observatory
swampler@noao.edu